package com.ilas.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.ilas.aspect.DataSource;
import com.ilas.common.domain.AjaxResult;
import com.ilas.config.DynamicDataSourceContextHolder;
import com.ilas.entity.BizUser;
import com.ilas.service.UserService;
import com.ilas.service.impl.TenantInfoServiceImpl;
import com.ilas.service.impl.TenantServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>${Description}</p>
 *
 * @author luzck
 * @date 2021/4/1 9:42
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private TenantInfoServiceImpl tenantInfoService;

    @Resource
    private TenantServiceImpl tenantService;

    @Value("${tenant.version}")
    private String tv;

    /**
     * 分别查询master和slave数据库所有注册用户
     * @return master数据库下所有注册用户
     */
    @RequestMapping("all")
    public List<BizUser> findAll(){

        return userService.getList();
    }

    /**
     * 默认数据源master查询所有注册用户
     * @return master数据库下所有注册用户
     */
    @RequestMapping("master_all")
    @DataSource()
    public List<BizUser> findAll2(){
        return userService.getList2();
    }

    /**
     * 默认数据源slave查询所有注册用户
     * @return slave数据库下所有注册用户
     */
    @RequestMapping("slave_all")
    @DataSource("slave")
    public List<BizUser> findAll3(){
        return userService.getList2();
    }

    /**
     * 将租户信息作为请求参数传递给服务端
     * 指定数据源查询所有注册用户
     * @return 指定数据库下所有注册用户
     */
    @RequestMapping("customize_all")
    public AjaxResult customize_all(String tenantId) {
        if (StringUtils.isEmpty(tenantId)) {
            tenantId = "master";
        }

        // 检查租户编号是否存在
        boolean flag = true;
        // master库不需要校验
        if (!"master".equals(tenantId)) {
            if ("v1.0".equals(tv)) {
                flag = tenantInfoService.isExist(tenantId);
            } else {
                flag = tenantService.isExsiteCode(tenantId);
            }
        }
        if (flag) {
            DynamicDataSourceContextHolder.clearDataSourceKey();
            DynamicDataSourceContextHolder.setDataSourceKey(tenantId);

            return AjaxResult.success(userService.getList2());
        }

        return AjaxResult.error("租户不存在");
    }

    /**
     * 根据域名去查询租户下的所有注册用户
     * @return 查询租户下的所有注册用户
     */
    @RequestMapping("/customize_all/domain")
    public AjaxResult customize_all(HttpServletRequest request) {
        String host = request.getRemoteHost();
        log.info("域名---> {}", host);
        String tenantCode = tenantService.getTenantCodeByDomainName(host);
        if (StringUtils.isEmpty(tenantCode)){
            return AjaxResult.error("指定认证后的域名再进行操作");
        }
        DynamicDataSourceContextHolder.clearDataSourceKey();
        DynamicDataSourceContextHolder.setDataSourceKey(tenantCode);

        return AjaxResult.success(userService.getList2());
    }

    /**
     * 往slave数据库注册一条用户数据
     * @return 影响结果
     */
    @RequestMapping("slave_insert")
    public AjaxResult insert(){
        DynamicDataSourceContextHolder.setDataSourceKey("slave");
        BizUser user = new BizUser();
        user.setName("我是从数据库的新用户");
        user.setUsername("slave_luzck");
        user.setPassword("123");
        userService.insertUser(user);

        return AjaxResult.success();
    }

    /**
     * 往master数据库注册一条用户数据
     * @return 影响结果
     */
    @RequestMapping("master_insert")
    @DataSource()
    public AjaxResult insert2(){
        BizUser user = new BizUser();
        user.setName("我是主数据库的新用户");
        user.setUsername("master_luzck");
        user.setPassword("123");
        userService.insertUser(user);

        return AjaxResult.success();
    }

}
